//
//  CTFollowUserLogic.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/6.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTFollowUserLogic.h"
#import "CTDebugManager.h"
#import "CTRequestUtil.h"
#import "CTUrl.h"
#import "CTFile.h"
#import "CTRequestErrorCode.h"

@implementation CTFollowUserLogic

- (void)dealloc
{
    [CTDebugManager showDebugLogInfo:@"dealloc : CTFollowUserLogic Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}



#pragma mark - request -


-(void)followUserListRequest:(CTRequestFollowUserItem *)item
    successCompletionHandler:(requestSuccessBlock)successBlock
               failedHandler:(requestFailedBlock)failedBlock
{
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithCapacity:0];
    @try {
        [param setObject:item.token forKey:@"token"];
        [param setObject:item.lang forKey:@"lang"];
        [param setObject:item.count forKey:@"count"];
        [param setObject:item.visitor forKey:@"visitor"];
        [param setObject:item.timelimit forKey:@"timelimit"];
        [param setObject:item.uid forKey:@"uid"];
        [param setObject:item.page forKey:@"page"];
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"组装关注数据错误！"];
    }
    
    [CTRequestUtil sendAsyGetRequest:CT_URL_USERS_FOLLOW_LIST(version) withParam:param withCompletionSuccessHandler:^(NSData *data) {
        BOOL isParserSuccess = [self parserFollowListData:data];
        if (isParserSuccess == YES) {
            successBlock();
        }
        else
        {
            failedBlock();
        }
        
    } withFailedHandler:^(NSError *error) {
        failedBlock();
    }];
}





#pragma mark - data parser-

-(BOOL)parserFollowListData:(NSData *)resultData
{
    
    BOOL isParserSuccess = NO;
    
    @try {
        
        NSError * error;
        
        id jsonObject = [NSJSONSerialization
                         JSONObjectWithData:resultData
                         options:NSJSONReadingAllowFragments
                         error:&error];
        if (jsonObject != nil && error == nil){
            
            if ([jsonObject isKindOfClass:[NSDictionary class]]){
                
                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
                NSString * errCode = [NSString stringWithFormat:@"%@",(NSString *)[deserializedDictionary objectForKey:@"error_code"]];
                if ([errCode isEqualToString:@"0"]) {
                    
                    isParserSuccess = YES;
                    NSDictionary * infoDic = [deserializedDictionary objectForKey:@"info"];
                    
                    //存入文件
                    
                    [CTFile cacheDicFile:[infoDic mutableCopy] fileName:@"followUserList.plist" saveDoc:CTFileDocLibrary savePath:@"cacheData/user"];
                    
                }
                else{
                    NSString * errDescription = [CTRequestErrorCode errDescription:errCode];
                    [CTDebugManager showErrorLogInfo:errDescription];
                }
            }
        }
        else
        {
            [CTDebugManager showErrorLogInfo:@"解析关注的用户列表数据错误！"];
        }
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"解析关注数的用户列表据错误！"];
    }
    
    return isParserSuccess;
    
}




@end
